package com.sky.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

// 在Spring Boot中配置Druid连接池参数
@Configuration
public class DataSourceConfig {
    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/sky_take_out?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("jennie...");

        // 设置连接池的最大空闲时间
        dataSource.setMaxWait(60000); // 单位是毫秒，这里设置为60秒
        dataSource.setTimeBetweenEvictionRunsMillis(60000); // 检测连接是否有效的时间间隔
        dataSource.setMinEvictableIdleTimeMillis(300000); // 最小空闲时间，单位是毫秒，这里设置为5分钟

        return dataSource;
    }
}
